//
//  TodayHotViewController.m
//  CapriceMusic
//
//  Created by 蒋恩浩 on 16/1/6.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "TodayHotViewController.h"
#import "TodayHotModel.h"
#import "TodayHotTableViewCell.h"
@interface TodayHotViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *modelArray;

@property (nonatomic, strong) UISegmentedControl *segmentControl;

//网络请求参数
@property (nonatomic, copy) NSString *urlStr;
@property (nonatomic, assign) NSInteger num;

@end

@implementation TodayHotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelArray = [NSMutableArray arrayWithCapacity:0];
    self.navigationItem.title = @"音悦节目";
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"精选",@"热门",@"最新"]];
    _segmentControl.frame = CGRectMake(self.view.frame.size.width / 10, 0, self.view.frame.size.width * 0.8, 30);
    _segmentControl.tintColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    self.segmentControl.selectedSegmentIndex = 0;
    [self.segmentControl addTarget:self action:@selector(segmentControlDidPress) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentControl];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, self.view.frame.size.height - 94) style:UITableViewStyleGrouped];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor blackColor];
    [self.tableView registerClass:[TodayHotTableViewCell class] forCellReuseIdentifier:@"TodayHotTableViewCellIdentifier"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self getData:@"CHOICE&offset=0&size=20"];
    self.urlStr = @"CHOICE";
    [self getHeaderRefresh];
    [self getFootRefresh];
}

- (void)segmentControlDidPress {
    if (self.segmentControl.selectedSegmentIndex == 0) {
        NSString *str = @"CHOICE&offset=0&size=20";
        self.urlStr = @"CHOICE";
        [self getData:str];
    } else if (self.segmentControl.selectedSegmentIndex == 1) {
        NSString *str = @"HOT&offset=0&size=20";
        self.urlStr = @"HOT";
        [self getData:str];
    } else if (self.segmentControl.selectedSegmentIndex == 2) {
        NSString *str = @"NEW&offset=0&size=20";
        self.urlStr = @"NEW";
        [self getData:str];
    }
}

- (void)getHeaderRefresh {
    __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithCallback:^{
        NSString *str = [NSString stringWithFormat:@"%@&offset=0&size=20", weakSelf.urlStr];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.modelArray removeAllObjects];
            [weakSelf getData:str];
            [weakSelf.tableView headerEndRefreshing];
        });
    }];
}

- (void)getFootRefresh {
    __weak typeof(self) weakSelf = self;
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.num += 20;
            NSString *str = [NSString stringWithFormat:@"%@&offset=%ld&size=20", weakSelf.urlStr, weakSelf.num];
            [JEHNetWorkTool getWithUrl:[MusicListPageGETURL stringByAppendingString:str] parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
                NSArray *arr = result[@"playLists"];
                for (NSDictionary *dic in arr) {
                    TodayHotModel *todayModel = [[TodayHotModel alloc] initWithDictionary:dic];
                    [weakSelf.modelArray addObject:todayModel];
                }
                [weakSelf.tableView reloadData];
            } fail:^(NSError *error) {
                
            }];
            [weakSelf.tableView footerEndRefreshing];
        });
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TodayHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TodayHotTableViewCellIdentifier"];
    if (self.modelArray.count) {
        cell.todayHotModel = self.modelArray[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 2.5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    TodayHotModel *mvpageModel = self.modelArray[indexPath.row];
    detailVC.appendingId = mvpageModel.ids;
    detailVC.type = @"PLAYLIST";
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)getData:(NSString *)str {
    [ProgressHUD show:@"正在加载"];
    [self.modelArray removeAllObjects];
    [JEHNetWorkTool getWithUrl:[MusicListPageGETURL stringByAppendingString:str] parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"playLists"];
        for (NSDictionary *dic in arr) {
            TodayHotModel *todayModel = [[TodayHotModel alloc] initWithDictionary:dic];
            [self.modelArray addObject:todayModel];
        }
        [ProgressHUD dismiss];
        [_tableView reloadData];
        [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    } fail:^(NSError *error) {
        [ProgressHUD showError:@"请检查网络"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
